'use strict';

let path = require('path');
let webpack = require('webpack');

let baseConfig = require('./base');
let defaultSettings = require('./defaults');

// Add needed plugins here
let BowerWebpackPlugin = require('bower-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');

let config = Object.assign({}, baseConfig, {
  env: 'dist',
  entry: {
    index: path.join(__dirname, '../src/index'),
    common: ['react', 'redux', 'react-redux', 'react-router-redux', 'react-router', 'redux-thunk', 'whatwg-fetch', 'react-chartjs']
  },
  output: {
    path: path.join(__dirname, '../dist'),
    filename: '[name]-[chunkhash:7].js'//不使用[name]，并且插件中没有filename，
    //这输出文件中只用chunk.js的内容，main.js的内容不知跑哪里去了
  },
  cache: false,
  devtool: 'sourcemap',
  plugins: [
    new webpack.optimize.DedupePlugin(),
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': '"production"'
    }),
    new BowerWebpackPlugin({
      searchResolveModulesDirectories: false
    }),
    new webpack.optimize.UglifyJsPlugin(),
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.optimize.AggressiveMergingPlugin(),
    new webpack.NoErrorsPlugin(),
    new webpack.optimize.CommonsChunkPlugin({
      name: ['common'], // 注意不要.js后缀
      filename: '[name]-[chunkhash:7].js',
      // chunks:['main','user','index']
    }),
    new HtmlWebpackPlugin({
      filename: path.join(__dirname, '../dist/index.html'),
      template: path.join(__dirname, '../src/index.html'),
      inject: 'head',
      chunks: ['common' ,'index']

    })
  ],
  module: defaultSettings.getDefaultModules()
});

// Add needed loaders to the defaults here
config.module.loaders.push({
  test: /\.(js|jsx)$/,
  loader: 'babel',
  include: [].concat(
    config.additionalPaths,
    [ path.join(__dirname, '/../src') ]
  )
});

module.exports = config;
